package com.pattern.strategy;

/**
 * @author Mr.yuan
 * @version v1.0
 * @date 2019/7/25 19:46
 **/
public class StrategyPattern {

    public static void main(String[] args) {
        StrategyContext context = new StrategyContext(new operationAdd());
        System.out.println("add =="+context.executeStrategy(10, 5));
        context = new StrategyContext(new operationSubstract());
        System.out.println("substract =="+context.executeStrategy(10, 5));
        context = new StrategyContext(new operationMulti());
        System.out.println("multi =="+context.executeStrategy(10, 5));
    }

    private interface Strategy{
        int doOperation(int a,int b);
    }
    private static class operationAdd implements Strategy{
        @Override
        public int doOperation(int a, int b) {
            return a+b;
        }
    }
    private static class operationSubstract implements Strategy{
        @Override
        public int doOperation(int a, int b) {
            return a-b;
        }
    }
    private static class operationMulti implements Strategy{
        @Override
        public int doOperation(int a, int b) {
            return a*b;
        }
    }

    private static class StrategyContext{
        private Strategy strategy;

        public StrategyContext(Strategy strategy) {
            this.strategy = strategy;
        }

        private int executeStrategy(int a,int b){
            return strategy.doOperation(a,b);
        }
    }
}
